#ifndef _LOG_RINGBUFFER_H__
#define _LOG_RINGBUFFER_H__

#include <stddef.h>
#include <string.h>
#include <stdint.h>

struct log_ringbuffer
{
    char *buffer_ptr;

    uint16_t read_mirror;
    uint16_t read_index;
    uint16_t write_mirror;
    uint16_t write_index;

    uint32_t buffer_size;
};

enum ringbuffer_state
{
    RINGBUFFER_EMPTY,
    RINGBUFFER_FULL,
    RINGBUFFER_HALFFULL,
};

int ringbuffer_init(struct log_ringbuffer *rb, char *buffer_ptr, uint32_t size);
int ringbuffer_deinit(struct log_ringbuffer *rb);

uint32_t ringbuffer_put(struct log_ringbuffer *rb, const char *ptr, uint32_t length);
uint32_t ringbuffer_put_force(struct log_ringbuffer *rb, const char *ptr, uint32_t length);

uint32_t ringbuffer_get(struct log_ringbuffer *rb, char *ptr, uint32_t length);
uint32_t ringbuffer_peek(struct log_ringbuffer *rb, char **ptr);

uint32_t ringbuffer_data_len(struct log_ringbuffer *rb);

inline uint32_t ring_buffer_size_get(struct log_ringbuffer *rb)
{
    uint32_t size = 0;

    if(rb != NULL)
    {
        size = rb->buffer_size;
    }

    return size;
}
#define ringbuffer_space_len(rb) ((rb)->buffer_size - ringbuffer_data_len(rb))

#endif
